package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.system.insurance.info.SupplierSystemOrderInsuranceDetailInfoDTO;
import com.ctshk.rpc.booking.dto.system.insurance.info.SupplierSystemOrderInsuranceInfoDTO;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderInsurance;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderInsuranceDetail;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderInsuranceMapper;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderInsuranceDetailMapper;
import com.ctshk.rpc.booking.req.system.insurance.add.SupplierSystemOrderInsuranceAddReq;
import com.ctshk.rpc.booking.req.system.insurance.add.SupplierSystemOrderInsuranceDetailAddReq;
import com.ctshk.rpc.booking.service.ISupplierSystemOrderInsuranceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 系统内保险基础信息 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-09
 */
@DubboService
public class SupplierSystemOrderInsuranceServiceImpl implements ISupplierSystemOrderInsuranceService {
    @Autowired
    SupplierSystemOrderInsuranceMapper supplierSystemOrderInsuranceMapper;
    @Autowired
    SupplierSystemOrderInsuranceDetailMapper supplierSystemOrderInsuranceDetailMapper;

    @Override
    public void add(Long id, Long userId, LocalDateTime nowDateTime, SupplierSystemOrderInsuranceAddReq supplierSystemOrderInsuranceAddReq) {
        if (supplierSystemOrderInsuranceAddReq != null) {
            SupplierSystemOrderInsurance supplierSystemOrderInsurance = EntityUtil.copy(supplierSystemOrderInsuranceAddReq, SupplierSystemOrderInsurance.class);
            supplierSystemOrderInsurance.setId(SnowflakeIdWorker.nextId());
            supplierSystemOrderInsurance.setOrderId(id);
            supplierSystemOrderInsurance.setCreateId(userId);
            supplierSystemOrderInsurance.setGmtCreate(nowDateTime);
            supplierSystemOrderInsurance.setModifiedId(userId);
            supplierSystemOrderInsurance.setGmtModified(nowDateTime);
            supplierSystemOrderInsurance.setIsDeleted(IsDeletedCode.NO.getCode());
            supplierSystemOrderInsuranceMapper.insert(supplierSystemOrderInsurance);
            //添加旅客
            List<SupplierSystemOrderInsuranceDetailAddReq> supplierSystemOrderInsuranceDetailAddReqList = supplierSystemOrderInsuranceAddReq.getSupplierSystemOrderInsuranceDetailAddReqList();
            if (CollectionUtils.isNotEmpty(supplierSystemOrderInsuranceDetailAddReqList)) {
                supplierSystemOrderInsuranceDetailAddReqList.forEach(trainTouristAdd -> {
                    SupplierSystemOrderInsuranceDetail supplierSystemOrderInsuranceDetail = EntityUtil.copy(trainTouristAdd, SupplierSystemOrderInsuranceDetail.class);
                    supplierSystemOrderInsuranceDetail.setId(SnowflakeIdWorker.nextId());
                    supplierSystemOrderInsuranceDetail.setOrderId(id);
                    supplierSystemOrderInsuranceDetail.setInsuranceId(supplierSystemOrderInsurance.getId());
                    supplierSystemOrderInsuranceDetail.setCreateId(userId);
                    supplierSystemOrderInsuranceDetail.setGmtCreate(nowDateTime);
                    supplierSystemOrderInsuranceDetail.setModifiedId(userId);
                    supplierSystemOrderInsuranceDetail.setGmtModified(nowDateTime);
                    supplierSystemOrderInsuranceDetail.setIsDeleted(IsDeletedCode.NO.getCode());
                    supplierSystemOrderInsuranceDetailMapper.insert(supplierSystemOrderInsuranceDetail);
                });
            }
        }
    }

    @Override
    public List<SupplierSystemOrderInsuranceInfoDTO> selectByOrderId(Long id) {
        QueryWrapper<SupplierSystemOrderInsurance> trainQueryWrapper = new QueryWrapper<>();
        trainQueryWrapper.lambda().eq(SupplierSystemOrderInsurance::getOrderId, id);
        List<SupplierSystemOrderInsurance> supplierSystemOrderInsuranceList = supplierSystemOrderInsuranceMapper.selectList(trainQueryWrapper);
        List<SupplierSystemOrderInsuranceInfoDTO> supplierSystemOrderInsuranceInfoDTOList = EntityUtil.copyList(supplierSystemOrderInsuranceList, SupplierSystemOrderInsuranceInfoDTO.class);
        if (CollectionUtils.isNotEmpty(supplierSystemOrderInsuranceInfoDTOList)) {
            supplierSystemOrderInsuranceInfoDTOList.forEach(trainCheckinDTO -> {
                //添加旅客
                QueryWrapper<SupplierSystemOrderInsuranceDetail> trainTouristQueryWrapper = new QueryWrapper<>();
                trainTouristQueryWrapper.lambda().eq(SupplierSystemOrderInsuranceDetail::getOrderId, id);
                List<SupplierSystemOrderInsuranceDetail> supplierSystemOrderInsuranceDetails = supplierSystemOrderInsuranceDetailMapper.selectList(trainTouristQueryWrapper);
                List<SupplierSystemOrderInsuranceDetailInfoDTO> supplierSystemOrderInsuranceDetailInfoDTOS = EntityUtil.copyList(supplierSystemOrderInsuranceDetails, SupplierSystemOrderInsuranceDetailInfoDTO.class);
                trainCheckinDTO.setSupplierSystemOrderInsuranceDetailInfoDTOList(supplierSystemOrderInsuranceDetailInfoDTOS);
            });
        }
        return supplierSystemOrderInsuranceInfoDTOList;
    }
}
